// pages/login/index.js
import {
  login
} from "../../apis/login_api"
Page({
  /**
   * 页面的初始数据
   */
  data: {
    templateId1:'wYVUWPGQsdxINoc3VIKllS-yY7c_FVJx_Ma_9nWZUoE'
  },
  // 获取手机号
  getPhoneNumber(e) {
    // wx.requestSubscribeMessage({
    //   tmplIds: ['nv1mxHUoxpWxfIZv4Z6oUh9PdP5MWnP5Vl39OCIiA8k'],
    //   complete (res) { 
    //     console.log('11')
    //     console.log(res)
    //   }
    // })
    if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
      wx.showToast({
        icon: "none",
        title: '请允许获取手机号，否则功能不可用！',
      })
    } else {
      console.log(e.detail.code,'======');
      login(e.detail.code).then(res => {
        if (res.success) {

          console.log(res)
          wx.setStorage({
            key: "token",
            data: res.data.token
          })
          let phone = res.data.loginResult.phone;
          let name = res.data.loginResult.name;
          let address = res.data.loginResult.address
          let authority = res.data.loginResult.authority
          let addressList = res.data.loginResult.addressList
          wx.setStorage({
            key: "phoneNumber",
            data: phone
          })
          wx.setStorage({
            key: "name",
            data: name
          })
          wx.setStorage({
            key: "address",
            data: address
          })
          wx.setStorage({
            key: "code",
            data: authority
          })
          wx.setStorage({
            key: "addressList",
            data: addressList
          })
          // 打印具体的数据
          console.log("phone:", phone);
          console.log("name:", name);
          console.log("address:", address);
          console.log("authority:", authority);
          console.log("addressList:", addressList);
          wx.switchTab({
            url: '/pages/map/map',
          })
           
        } else {
          wx.showToast({
            icon: "error",
            title: res.message,
          })
        }
      }).catch(res => {
        wx.showToast({
          icon: "error",
          title: '请重试！',
        })
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    let phone = wx.getStorageSync('phoneNumber')
    if (phone.length == 11) {
      wx.switchTab({
        url: '/pages/map/map'
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})